package com.baizhi.controller;

import com.baizhi.entity.Tree;
import com.baizhi.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("tree")
class TreeController {

    @Autowired
    private TreeService treeService;

    @ResponseBody
    @RequestMapping("show")
    public List<Tree> showAll(){
        List<Tree> trees = treeService.showAll();
        for (Tree tree:trees){
            if (tree.getParentId() != null){

                for (Tree tree2:trees){
                    if (tree2.getId() == tree.getParentId()){
                        if (tree2.getChildren() == null){
                            tree2.setChildren(new ArrayList<Tree>());
                        }
                        tree2.getChildren().add(tree);
                    }
                }

            }
        }

        List<Tree> list = new ArrayList<Tree>();
        for (Tree tree:trees){
            if (tree.getParentId()== null){
                list.add(tree);

            }
        }

        return list;


    }
}



